package com.example.tokendemo.handler;

import com.example.tokendemo.model.LoginUser;
import com.example.tokendemo.model.User;
import javafx.scene.effect.SepiaTone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

//    @Autowired
//    private PasswordEncoder encoder;
//    @Autowired
//    private BCryptPasswordEncoder encoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if(!username.equals("admin")){
            throw new UsernameNotFoundException("用户名不存在");
        }

        String password = "123"; //encoder.encode("123");""
        //password = encoder.encode("123");

        Set<String> list = new HashSet<>();
        list.add("normal");
        User user = new User();
        user.setPlayer_name(username);
        user.setPassword(password);
        //List<GrantedAuthority> list = AuthorityUtils.createAuthorityList("admin", "normal");
        //return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
        return new LoginUser(user, list);

    }
}
